package sfeir.gwt.ergosum.client.profile;

import java.util.List;

import sfeir.gwt.ergosum.client.images.ErgoImages;
import sfeir.gwt.ergosum.client.images.NetworksImage;
import sfeir.gwt.ergosum.client.model.Address;
import sfeir.gwt.ergosum.client.model.Email;
import sfeir.gwt.ergosum.client.model.ImageURL;
import sfeir.gwt.ergosum.client.model.NetworkItem;
import sfeir.gwt.ergosum.client.model.Person;
import sfeir.gwt.ergosum.client.model.Tel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ViewProfile extends VerticalPanel {

    private Person mPerson;
    private VerticalPanel pro, perso, other;
    
    public ViewProfile(Person person) {
       mPerson = person;
       String username = mPerson.getAlias();
       setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
       setWidth("100%");
       HorizontalPanel allInformation = new HorizontalPanel();
       allInformation.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
       allInformation.setWidth("100%");
       Image image = new Image();
       image.setPixelSize(80, 100);
       image.setHeight("100px");
       image.setUrl("/images/person.png");
       ImageURL imgURL = mPerson.getPhoto();
       if (imgURL != null && imgURL.getImageUrl() != null && !imgURL.getImageUrl().equals(""))
           image.setUrl(mPerson.getPhoto().getImageUrl());
       allInformation.add(image);
       
       //Create the information list
       VerticalPanel vp = new VerticalPanel();
       Label name = new Label();
       String fullname = mPerson.getFullName();
       if (mPerson.getNickname() != null)
           fullname += " (" + mPerson.getNickname() + ")";
       name.setText(fullname);
       vp.add(name);
       if (mPerson.getTitle() != null)
           vp.add(new Label(mPerson.getTitle()));
       if (mPerson.getCompany() != null)
           vp.add(new Label(mPerson.getCompany()));
       if (mPerson.getBirthDay() != null)
           vp.add(new Label(mPerson.getBirthDay()));
       allInformation.add(vp);
       add(allInformation);
       
       //Create the Tab
       TabPanel tabpanel = new DecoratedTabPanel();
       tabpanel.setAnimationEnabled(true);
       tabpanel.setWidth("100%");
       tabpanel.add(createTabNetworks(mPerson), "Networks");
       tabpanel.add(createTabAboutMe(mPerson), "About me");
       tabpanel.add(createTabContact(mPerson), "Contact");
       tabpanel.selectTab(0);
       add(tabpanel);
       String url = "http://" + Window.Location.getHost() + "/profile/" +  username;
       HTML profile_link = new HTML("This profile is accessible by the url <a href=\"" + url +"\" >" + url + "</a>");
       add(profile_link);
       Anchor link = new Anchor("Add with HiveGotContacts", "ergo://ergosum/" + username);
       add(link);
       Anchor linkVcard = new Anchor("Download in vcard", "http://" + Window.Location.getHost() + "/vcard/" +  username);
       add(linkVcard);
    }

    private Widget createTabAboutMe(Person person) {
        return new HTML(person.getAboutMe());
    }

    private Widget createTabContact(Person person) {
        HorizontalPanel panel = new HorizontalPanel();
        panel.setSpacing(4);
        
        pro = new VerticalPanel();
        pro.setSpacing(4);
        	HTML labelPro = new HTML("<b>Professionnel :</b>");
        	pro.add(labelPro);
        perso = new VerticalPanel();
        perso.setSpacing(4);
        	HTML labelPerso = new HTML("<b>Personnel :</b>");
        	perso.add(labelPerso);
        other = new VerticalPanel();
        other.setSpacing(4);
        	HTML labelOther = new HTML("<b>Autres :</b>");
        	other.add(labelOther);
        
        panel.add(pro);
        panel.add(perso);
        panel.add(other);
        
        createPanelTels(person.getTels());
        createPanelEmails(person.getEmails());
        createPanelAdress(person.getAddresses());
        
        return panel;
    }
    
    private void createPanelEmails(List<Email> list) {
        ErgoImages images = (ErgoImages)GWT.create(ErgoImages.class);
        for (Email item : list) {
            HorizontalPanel hp = new HorizontalPanel();
            hp.setWidth("250px");
            hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
            hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
            int type = item.getTypeEmail();
            AbstractImagePrototype img = images.email_2();
            if (type == Email.PERSONAL_EMAIL)
                img = images.email_perso();
            else if (type == Email.PROFESSIONNAL_EMAIL)
                img = images.email_pro_2();
            hp.add(img.createImage());
            String url = item.getEmail();
            Anchor anchor = new Anchor(url, "mailto:" + url);
            hp.add(anchor);
            hp.setCellWidth(anchor, "200px");
            
            if (type == Email.PERSONAL_EMAIL)
            	perso.add(hp);
            else if (type == Email.PROFESSIONNAL_EMAIL)
            	pro.add(hp);
            else if((type == Email.UNKNOWN))
            	other.add(hp);
        }
    }

    private void createPanelTels(List<Tel> list) {
        ErgoImages images = (ErgoImages)GWT.create(ErgoImages.class);
        for (Tel item : list) {
            HorizontalPanel hp = new HorizontalPanel();
            hp.setWidth("250px");
            hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
            hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
            int type = item.getType();
            AbstractImagePrototype img = images.phone();
            if (type == Tel.HOME_LANDLINE)
                img = images.phone_perso();
            else if (type == Tel.MOBILE_LINE)
                img = images.phone_mobile();
            else if (type == Tel.PROFESSIONNAL_LINE)
                img = images.phone_pro();
            hp.add(img.createImage());
            String url = item.getPhoneNumber();
            Anchor anchor = new Anchor(url, "callto:" +url);
            hp.add(anchor);
            hp.setCellWidth(anchor, "200px");
            if (type == Tel.HOME_LANDLINE)
            	perso.add(hp);
            else if (type == Tel.PROFESSIONNAL_LINE)
            	pro.add(hp);
            else if((type == Tel.MOBILE_LINE))
            	other.add(hp);
        }
    }

    private void createPanelAdress(List<Address> list) {
        ErgoImages images = (ErgoImages)GWT.create(ErgoImages.class);
        for (Address item : list) {
            HorizontalPanel hp = new HorizontalPanel();
            hp.setWidth("250px");
            hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
            hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
            int type = item.getType();
            AbstractImagePrototype img = images.house_2();
            if (type == Address.PROFESSIONNAL_ADDRESS)
                img = images.work();
            hp.add(img.createImage());
            String url = item.getFullValue();
            Anchor anchor = new Anchor(url, "http://maps.google.fr/?q=" + URL.encodeComponent(url));
            hp.add(anchor);
            hp.setCellWidth(anchor, "200px");
            if (type == Address.PERSONAL_ADDRESS)
            	perso.add(hp);
            else if (type == Address.PROFESSIONNAL_ADDRESS)
            	pro.add(hp);
            else if((type == Address.DEFAULT_ADDRESS))
            	other.add(hp);
        }
    }

    private Widget createTabNetworks(Person person) {
        FlowPanel panel = new FlowPanel();
        List<NetworkItem> list = person.getNetworkItems();
        NetworksImage images = (NetworksImage)GWT.create(NetworksImage.class);
        for (NetworkItem item : list) {
            HorizontalPanel hp = new HorizontalPanel();
            hp.setWidth("350px");
            hp.setHeight("50px");
            hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
            hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
            VerticalPanel desc = new VerticalPanel();
            
            String type = item.getNetworkItemType();
            String url = item.getNetworkItemLink();
            
            AbstractImagePrototype img = images.web();
            if (type.equalsIgnoreCase("linkedin"))
                img = images.linkedin();
            else if (type.equalsIgnoreCase("twitter"))
                img = images.twitter();
            else if (type.equalsIgnoreCase("facebook"))
                img = images.facebook();
            else if (type.equalsIgnoreCase("viadeo"))
                img = images.viadeo();
            else if (type.equalsIgnoreCase("skype"))
                img = images.skype();
            else if (type.equalsIgnoreCase("flickr"))
                img = images.flickr();
            else if (type.equalsIgnoreCase("vimeo"))
                img = images.vimeo();
            else if (type.equalsIgnoreCase("dopplr"))
                img = images.dopplr();
            else if (type.equalsIgnoreCase("lastfm"))
                img = images.lastfm();
            else if (type.equalsIgnoreCase("readernaut"))
                img = images.readernaut();
            else if (type.equalsIgnoreCase("friendfeed"))
                img = images.friendfeed();
            hp.add(img.createImage());
            
            HTML title = new HTML("<b>"+type.toUpperCase()+"</b>");
            desc.add(title);
            Anchor anchor = new Anchor(url, url);
            desc.add(anchor);
            hp.add(desc);
            hp.setCellWidth(desc, "300px");
            
            panel.add(hp);
        }           
        return panel;
    }

}
